package site.icesky.springboot.threadpool.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: FileUtil <br>
 * @Description: 文件操作的常用方法 <br>
 * @Package: site.icesky.springboot.threadpool.util <br>
 * @author: suitianmou <br>
 * @Date: 2020/8/20-12:04 <br>
 * @version: 1.0 <br>
 * @see <br>
 * @since (从产品的哪一个版本此类被添加进来 ( 可选))<br>
 * 修改历史: <br>
 * 修改时间             修改人           版本变更           修改说明<br>
 * -----------------------------------------------------<br>
 * <br>
 */
public class FileUtil {
    private static final Logger logger = LoggerFactory.getLogger(FileUtil.class);

    public static int getFileLine( String filePathName){
        try (LineNumberReader lineNumberReader = new LineNumberReader(new FileReader(filePathName))){
            lineNumberReader.skip(Long.MAX_VALUE);
            int lineNumber = lineNumberReader.getLineNumber();
            return lineNumber + 1;//实际上是读取换行符数量 , 所以需要+1
        } catch (IOException e) {
            return -1;
        }
    }
    public static List readFileByLine(String filePathName, int startLine, int step){
        List returnList  = new ArrayList<String>();

        File file = new File(filePathName);
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStreamReader in = new InputStreamReader(fileInputStream, "UTF-8");
            LineNumberReader reader = new LineNumberReader(in);

            String s="";
            int lines = 0;
            while( s!= null){
                lines++;
                s =  reader.readLine();
                //未到开始行数则跳过
                if( lines < startLine){
                    continue;
                }
                if( lines >= startLine + step){
                    break;
                }
                returnList.add(s);
            }
        } catch (IOException e) {
            logger.error("文件不存在!!!");
            return null;
        }

        return returnList;
    }
}
